/**
 *@author   wuwg
 *@createTime   2018/03/20
 *@updateTime   2018/04/28
 *@description  spin.js
 */
import Vue from 'vue'
import Spin from './spin/index.vue'
import config from '../../../config/config.js'
//  实例化
Spin.newInstance = properties => {
  const _props = properties || {}
  // 创建一个新的对象
  const Instance = new Vue({
    data: Object.assign({}, _props, {}),
    render (h) {
      let vnode = ''
      if (this.render) {
        vnode = h(Spin, {
          props: {
            fix: true,
            fullscreen: true
          }
        }, [this.render(h)])
      } else {
        vnode = h(Spin, {
          props: {
            size: 'large',
            fix: true,
            fullscreen: true
          }
        })
      }
      return h('div', {
        class: `${config.prefix}-spin-fullscreen ${config.prefix}-spin-fullscreen-wrapper`
      }, [vnode])
    }
  })
  // dom生成后插入到body后面
  const component = Instance.$mount()
  document.body.appendChild(component.$el)
  //  获取 spin 对象
  const spin = Instance.$children[0]

  return {
    show () {
      spin.visible = true
    },
    remove (cb) {
      spin.visible = false
      setTimeout(function () {
        spin.$parent.$destroy()
        const spinDom = document.getElementsByClassName(config.prefix + '-spin-fullscreen')[0]
        if (spinDom) {
          document.body.removeChild(spinDom)
        }
        cb()
      }, 500)
    },
    component: spin
  }
}

export default Spin
